package com.frxuexi.user.repository

import com.frxuexi.lib_base.room.AskDao
import com.frxuexi.lib_base.room.AskLeave
import com.frxuexi.lib_base.room.User
import com.frxuexi.lib_base.room.UserDao
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.withContext
import javax.inject.Inject

@OptIn
class AskRepository @Inject constructor(private val askDao:AskDao) {
    fun commitAsk(askLeave: AskLeave):Flow<Long>{
        return flow { emit(askDao.insert(askLeave)) }
    }

    suspend fun getUserAskList(uid:Int,page:Int,size:Int):List<AskLeave>?{
        return withContext(Dispatchers.IO){
            askDao.queryAll(uid,page,size)
        }
    }

    suspend fun getAskCont(uid:Int):Int{
        return withContext(Dispatchers.IO){
            askDao.getAskCount(uid)
        }
    }

    fun getNoApprove():Flow<List<AskLeave>?>{
        return flow { emit(askDao.queryNoApprove()) }
    }

    fun approve(askLeave: AskLeave):Flow<Int>{
        return flow { emit(askDao.update(askLeave)) }
    }
}